﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xiaov.SmartWeb.Model.ActiveRecord;
using Xiaov.SmartWeb.Infrastructure.ExtMethod;
using Xiaov.SmartWeb.Infrastructure.Common;

namespace Xiaov.SmartWeb.Application.SysAdmin.Json
{
    public partial class Dept : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string userdept=Request.QueryString["userdept"];
                Xiaov.SmartWeb.Model.ActiveRecord.DWBMB dept = new Model.ActiveRecord.DWBMB();                
                List<DWBMB> list = dept.FindAllByCache().ToList();
                
                if (Request.QueryString["t"] == "treegrid")
                {
                    Response.Write(list.ToDataTable().SortBy("SORTBY", "ASC").ToJsonForTreeGrid("DWBM", "PARENTID", "0"));
                    Response.End();
                }
                if (Request.QueryString["t"] == "combotree")
                {
                    Response.Write(list.ToDataTable().SortBy("SORTBY", "ASC").ToJsonForComboTree("DWBM", "DWMC", "PARENTID", "0", "0", true));
                    Response.End();
                }
                
                //用户管辖的机构

                if (Request.QueryString["t"] == "useradmin")
                {
                    if (!string.IsNullOrEmpty(userdept))
                    {
                        list = list.Where(s => s.DWBM.Contains(userdept)).ToList();
                    }
                    string rootdept = "0";
                    if (userdept.Length > 2)
                        rootdept = userdept.Remove(userdept.Length - 2);
                    Response.Write(list.ToDataTable().SortBy("SORTBY", "ASC").ToJsonForTreeGrid("DWBM", "PARENTID", rootdept));
                    Response.End();
                }

                //用户管辖的机构for combo
                if (Request.QueryString["t"] == "useradmincombo")
                {
                    if (!string.IsNullOrEmpty(userdept))
                    {
                        list = list.Where(s => s.DWBM.Contains(userdept)).ToList();
                    }
                    string rootdept = "0";
                    if (userdept.Length > 2)
                        rootdept = userdept.Remove(userdept.Length - 2);
                    Response.Write(list.ToDataTable().SortBy("SORTBY", "ASC").ToJsonForComboTree("DWBM", "DWMC", "PARENTID", rootdept, "0", true));
                    Response.End();
                }

            }
        }
    }
}